package com.darrenchan.course04

object ConstructorApp {
  def main(args: Array[String]): Unit = {
    val person111 = new Person2("陈驰", 25)
    println(person111.name + ":" + person111.age + ":" + person111.school)

    val person222 = new Person2("石泽涛", 23, "female")
    println(person222.name + ":" + person222.age + ":" + person222.school + ":" + person222.gender)

    val stu = new Student("魏印福", 100, "CS")
    println(stu.name + ":" + stu.major)


    println(stu)
  }
}

//主构造器
class Person2(val name: String, val age: Int) {
  println("Person Constructor enter...")

  val school = "neu"
  var gender: String = _

  //附属构造器
  def this(name: String, age: Int, gender: String) {
    this(name, age) //附属构造器的第一行代码必须调用主构造器或者其他附属构造器
    this.gender = gender
  }

  println("Person Constructor leave...")
}

class Student(name: String, age: Int, var major: String) extends Person2(name, age) {
  println("Student Constructor enter...")

  override val school = "nudt"
  override def toString: String = {
    this.name + ":" + this.age + ":" + this.major + ":" + school
  }

  println("Student Constructor leave...")
}
